package com.carryGo.web.dp;

import com.carryGo.entity.User;
import com.carryGo.service.dp.UserService;
import com.carryGo.utils.SendPhoneCodeUtil;
import com.carryGo.utils.WebUtils;
import com.carryGo.utils.dp.EncryptCodeUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * ClassName:LoginServlet
 * package:guoban
 * Desription:
 *
 * @Date:2020/8/23 17:29
 * @Author: 邓攀
 */
@WebServlet("/loginDp")
@MultipartConfig
public class LoginServlet extends HttpServlet {
    private UserService userService = new UserService();

    //request对象
    private HttpServletRequest request;
    //response对象
    private HttpServletResponse response;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //初始化请求和相应
        this.request = request;
        this.response = response;
        String method = request.getParameter("method");
        switch (method) {
            case "checkUsername": checkUsername(); break;
            case "regUser": regUser(); break;
            case "toLoginPage": toLoginPage(); break;
            case "logOut": logOut(); break;
            case "toRegPage": toRegPage(); break;
            case "sendCode": sendCode(); break;
            case "login": login(); break;
            case "findUser": findUser(); break;
            case "toFindPage": toFindPage(); break;
            case "toIndexPage": toIndexPage(); break;
            case "loginUsername": loginUsername(); break;
        }
    }

    private void toIndexPage() throws ServletException, IOException {

        WebUtils.toRequestPage(request,response,"index");
    }

    /**
     * 跳转到更改密码页面
     */
    private void toFindPage() throws ServletException, IOException {
        WebUtils.toRequestPage(request,response,"find");
    }

    /**
     * 修改密码
     */
    private void findUser() throws IOException {
        HttpSession session = request.getSession();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = userService.queryUserByUsername(username);
        user.setPassword(EncryptCodeUtils.getEncryptStr(password,user.getSalt()));
        boolean result = userService.updateUser(user);
        session.setAttribute("msg",result?"修改成功":"修改失败");
        response.sendRedirect("loginDp?method=toLoginPage");
    }


    /**
     * 登录验证
     */
    private void login() throws IOException {
        String msg = "";
        HttpSession session = request.getSession();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = userService.queryUserByUsername(username);
        if (user == null) {
            msg = "用户名不存在";
        } else {
            //先把传过来的密码加密一遍
            String encryptPwd = EncryptCodeUtils.getEncryptStr(password,user.getSalt());
            if (user.getPassword().equals(encryptPwd)) {
                session.setAttribute("user",user);
                response.sendRedirect("loginDp?method=toIndexPage");
                return;
            } else {
                msg = "用户名或密码错误";
            }
        }
        //返回错误信息
        session.setAttribute("msg",msg);
        response.sendRedirect("loginDp?method=toLoginPage");

//        User user = userService.queryUserByUsername(username);
//        WebUtils.sendMSG(response,(user!=null &&  (password == user.getPassword()))?"1":"-1");
    }

    /**
     * 获取验证码
     */
    private void sendCode() {
        String phone = request.getParameter("phone");
        SendPhoneCodeUtil sendPhoneCodeUtil = new SendPhoneCodeUtil(phone);
        sendPhoneCodeUtil.send();
        String code = sendPhoneCodeUtil.getCode();
        WebUtils.sendMSG(response,code);
    }

    /**
     * 跳转到登录页面
     */
    private void toLoginPage() throws ServletException, IOException {
        WebUtils.toRequestPage(request,response,"login");
    }

    /**
     * 跳转到注册页面
     */
    private void toRegPage() throws ServletException, IOException {
        WebUtils.toRequestPage(request,response,"reg");
    }

    /**
     * 跳转到协议页面
     */
    private void toProtocolPage() throws ServletException, IOException {
        WebUtils.toRequestPage(request,response,"protocol");
    }

    /**
     * 退出登录
     */
    private void logOut() throws IOException {
        //获取当前的session
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        session.setAttribute("msg","注销成功，请重新登录");
        response.sendRedirect("loginDp?method=toLoginPage");
    }

    /**
     * 注册用户
     */
    private void regUser() throws IOException {
        HttpSession session = request.getSession();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String salt = WebUtils.getRandomString();
        User user = new User(username,EncryptCodeUtils.getEncryptStr(password,salt),phone,salt);
        boolean result = userService.addUser(user);
        session.setAttribute("msg",result?"注册成功":"注册失败");
        //跳转到登录页面
        response.sendRedirect("loginDp?method=toLoginPage");
//        //获取当前的session
//        HttpSession session = request.getSession();
//        User user = WebUtils.getBean(User.class,request);
//        //设置盐值
//        user.setSalt(WebUtils.getRandomString());
//        //重新设置密码
//        user.setPassword(EncryptCodeUtils.getEncryptStr(user.getPassword(),user.getSalt()));
//        //插入数据
//        boolean result = userService.addUser(user);
//        //增加完之后，跳转到登录页面
//        session.setAttribute("msg",result?"注册成功":"注册失败");
//        session.setAttribute("result",result);
//        //跳转到登录页面
//        response.sendRedirect("login?method=toLoginPage");
    }

    /**
     * 验证用户名是否被注册
     */
    private void checkUsername() {
        String username = request.getParameter("username");
        User user = userService.queryUserByUsername(username);
        WebUtils.sendMSG(response, (user==null)?"1":"-1");
    }

    /**
     * 登录验证密码
     */
    private void loginUsername() {
        String username = request.getParameter("username");
        User user = userService.queryUserByUsername(username);
        String password = user.getPassword();
        WebUtils.sendMSG(response,password);
    }


}
